package org.example.book.controller;


import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.example.book.constants.Constants;
import org.example.book.enums.ResultStatus;
import org.example.book.model.*;
import org.example.book.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/book")
public class BookController {
    @Autowired
    private BookService bookService;


    @RequestMapping(value = "/addBook",produces = "application/json")
    public String addBook(BookInfo bookInfo){
            log.info("添加图书:{}",bookInfo);
            //参数校验
        if(!StringUtils.hasLength(bookInfo.getBookName())
        ||!StringUtils.hasLength(bookInfo.getAuthor())
        ||bookInfo.getCount()==null
        ||bookInfo.getPrice()==null
        ||!StringUtils.hasLength(bookInfo.getPublish())
        ||bookInfo.getStatus()==null){
            return "输入参数不合法，请检查入参";
        }
        try{
            bookService.addBook(bookInfo);
            return "";
        }catch(Exception e){
            log.error("添加图书失败！",e);
            return e.getMessage();
        }
    }
    //查询图书列表
    @RequestMapping("/getBookListByPage")
    public Result<PageResult<BookInfo>> getBookListByPage(PageRequest pageRequest, HttpSession session){
        log.info("查询图书列表，请求参数pageRequest:{}",pageRequest);
        //从session中获取用户信息
        //如果信息为空说明用户未登录
      /*  UserInfo loginUserInfo = (UserInfo) session.getAttribute(Constants.USER_SESSION_KEY);
        if(loginUserInfo == null || loginUserInfo.getId() < 0){
            Result.nologin();
        }*/
        //参数校验 补充
       /* if(pageRequest.getPageNum()==null) {
            return null;
        }*/
        PageResult<BookInfo> bookList = bookService.getBookListByPage(pageRequest);
            return Result.success(bookList);
    }

    @RequestMapping("/queryBookById")
    public BookInfo queryById(Integer bookId){
        log.info("根据ID查询图书信息,id:"+bookId);
        BookInfo bookInfo = bookService.queryBookById(bookId);
        return bookService.queryBookById(bookId);
    }

    @RequestMapping(value = "/updateBook",produces = "application/json")
    public String updateBook(BookInfo bookInfo){
        log.info("变更图书:{}" +bookInfo);
        try{
            Integer result =  bookService.updateBookById(bookInfo);
            if(result > 0 ){
                return "";
            }
            return "内部错误，请联系管理员";
        }catch(Exception e){
            log.error("更新图书失败,e:"+e);
            return "更新图书失败"+e.getMessage();
        }
    }

    @RequestMapping("/deleteBook")
    public Boolean deleteBook(Integer bookId){
        log.info("变更图书:{}" +bookId);

        Integer result =  bookService.deleteBook(bookId);
        if(result > 0 ){
            return true;
        }
            return false;
        
    }

    @RequestMapping(value = "/batchDelete",produces = "application/json")
    public String batchDelete ( @RequestParam List<Integer> ids){
        log.info("批量删除:{}"+ids);
        Integer result = bookService.batchDeleteByIds(ids);
        if(result > 0){
            return "";
        }else{
            return "删除失败,请联系管理员";
        }

    }


}
